package com.atguigu.gmall.seckill.controller;
import java.math.BigDecimal;

import com.atguigu.gmall.enums.OrderStatus;
import com.atguigu.gmall.enums.ProcessStatus;
import com.atguigu.gmall.feign.order.OrderFeignClient;
import com.atguigu.gmall.seckill.entity.SeckillGoods;
import com.google.common.collect.Lists;
import java.util.Date;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.common.result.ResultCodeEnum;
import com.atguigu.gmall.order.entity.OrderInfo;
import com.atguigu.gmall.order.vo.OrderSubmitVo;
import com.atguigu.gmall.seckill.service.SeckillBizService;
import com.atguigu.gmall.seckill.vo.SeckillOrderSubmitVo;
import org.simpleframework.xml.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author lfy
 * @Description
 * @create 2022-10-25 10:30
 */

@RequestMapping("/api/activity/seckill/auth")
@RestController
public class SeckillRestController {


    @Autowired
    SeckillBizService seckillBizService;

    @Autowired
    OrderFeignClient orderFeignClient;

    ///api/activity/seckill/auth/submitOrder
    @PostMapping("/submitOrder")
    public Result submitOrder(@RequestBody SeckillOrderSubmitVo submitVo){
        SeckillOrderSubmitVo.OrderDetailListDTO dto = submitVo.getOrderDetailList().get(0);
        //远程调用订单服务
        Long skuId = dto.getSkuId();
        //1、拿到redis中的订单
        OrderInfo orderInfo = seckillBizService.getSeckillOrder(skuId);

        SeckillGoods goodsDetail = seckillBizService.getGoodsDetail(skuId);

        //2、填充其他信息
        orderInfo.setConsignee(submitVo.getConsignee());
        orderInfo.setConsigneeTel(submitVo.getConsigneeTel());
        orderInfo.setOrderStatus(OrderStatus.UNPAID.name());
        orderInfo.setPaymentWay("online");
        orderInfo.setDeliveryAddress(submitVo.getDeliveryAddress());
        orderInfo.setOrderComment(submitVo.getOrderComment());
        orderInfo.setCreateTime(new Date());
        orderInfo.setExpireTime(new Date(System.currentTimeMillis() + 1000*60*30));
        orderInfo.setProcessStatus(ProcessStatus.UNPAID.name());
        orderInfo.setImgUrl(dto.getImgUrl());
        orderInfo.setOperateTime(new Date());
        orderInfo.setOriginalTotalAmount(goodsDetail.getPrice());
        orderInfo.setFeightFee(new BigDecimal("0"));
        orderInfo.setRefundableTime(new Date());


        //3、远程调用订单服务保存，并得到id即可
        Result<String> result = orderFeignClient.saveSeckillOrder(orderInfo);
        orderInfo.setId(Long.parseLong(result.getData()));

        //4、重新存到redis
        seckillBizService.updateSeckillOrder(orderInfo);


        return Result.ok(result.getData());//订单
    }

    /**
     * 获取秒杀码
     * @param skuId
     * @return
     */
    @GetMapping("/getSeckillSkuIdStr/{skuId}")
    public Result getSeckillSkuIdStr(@PathVariable("skuId") Long skuId){

        //为指定的商品生成一个秒杀码
        String code = seckillBizService.generateSeckillCode(skuId);

        return Result.ok(code);
    }


    /**
     * 秒杀下单; 排队。
     * @param skuId
     * @param skuIdStr
     * @return
     */
    @PostMapping("/seckillOrder/{skuId}")
    public Result seckillOrder(@PathVariable("skuId") Long skuId,
                               @RequestParam("skuIdStr") String skuIdStr){

        /**
         * 真正的秒杀：
         * 1）、生成秒杀订单，数据库保存了秒杀单【订单服务】
         * 2）、数据库库存扣减【秒杀服务】
         */
        // 200： 成功。 代表这个秒杀排队请求成功。 开始秒杀排队
        seckillBizService.queue(skuId,skuIdStr);

        return Result.ok();
    }


    /**
     * 检查秒杀单状态
     * @return
     */
    @GetMapping("/checkOrder/{skuId}")
    public Result checkOrder(@PathVariable("skuId") Long skuId){

        ResultCodeEnum codeEnum = seckillBizService.checkOrder(skuId);
        return Result.build("",codeEnum);
    }
}
